
module Volatility (
        parkinsonVol) where

import Types

testData = [BarData 2 3 0.5 1, BarData 1 1.5 0.5 1]

parkinsonVol :: [BarData]->Double
parkinsonVol bars = sqrt (k * sumed)
        where   k       = 1.0/4.0/(log 2.0)/(fromIntegral $ length bars)
                sumed   = foldl f 0.0 bars
                f a b   = a+(log ((barHigh b)/(barLow b)))**2



yangZhangVol :: [BarData]->Double
yangZhangVol bars = sqrt (s0 + k*sc + (1-k)*srs)
        where   n       = fromIntegral $ length bars
                k       = 0.34/(1+(n+1)/(n-1))
                s0      = 1/(n-1)*(fst $ foldl s0F (0.0, Nothing) bars)
                s0F (a, Nothing) b = (a, Just (barClose b))
                s0F (a, Just c)  b = (a+(log ((barOpen b)/c))**2, Just (barClose b))
                sc      = 1/(n-1)*(fst $ foldl scF (0.0, Nothing) bars)
                scF (a, Nothing) b = (a, Just (barOpen b))
                scF (a, Just c)  b = (a+(log ((barClose b)/c))**2, Just (barOpen b))
                srs     = 1/(n-1)*srsSum
                srsSum  = foldl srsF 0.0 bars
                srsF a b= a + (log ((barHigh b)/(barClose b)))*(log ((barHigh b)/(barOpen b))) +
                                (log ((barLow b)/(barClose b)))*(log ((barLow b)/(barOpen b))) 